import { ref, computed } from "vue";
import { multiplication, addition } from "@/utils/compute.js";
import { thousands } from "@/utils/index";
export const useSalesForm = (ruleForm) => {
  const bankList = ref([]);
  const storeList = ref([]);
  const carList = ref([]);
  const handleVinDel = (i, j) => {
    ruleForm.value.carList[i][0].vinList.splice(j, 1);
  };
  const handleVinAdd = (i) => {
    ruleForm.value.carList[i][0].vinList.push("");
  };
  const handleCarDel = (i) => {
    ruleForm.value.carList.splice(i, 1);
  };
  const handleCarAdd = () => { 
    ruleForm.value.carList.push([{ vinList: [""] }]);
  };
  const getCarItemsSum = computed(() => {
    return (item) => {
      const data = {
        num: 0,
        price: "0.00",
      };
      try {
        if (item.vinList) {
          data.num = item.vinList.length;
        }
        if (item.price) {
          data.price = thousands(multiplication(data.num, item.price));
        }
        return data;
      } catch (error) {
        return data;
      }
    };
  });
  const getCarListSum = computed(() => {
    const data = {
      num: 0,
      price: "0.00",
    };
    try {
      const list = ruleForm.value.carList.map((x) => {
        if (Array.isArray(x)) {
          return x[0];
        } else {
          return x;
        }
      });
      const nums = [];
      const prices = [];
      list.forEach((x) => {
        if (x.vinList) {
          nums.push(x.vinList.length);
        }
        if (x.price) {
          prices.push(multiplication(x.vinList.length, x.price));
        }
      });
      data.num = nums.reduce((sum, x) => {
        return addition(sum, x);
      }, 0);
      data.price = prices.reduce((sum, x) => {
        return addition(sum, x);
      }, 0);
      data.price = thousands(data.price);
      return data;
    } catch (error) {
      return data;
    }
  });
  const rules = ref({
    bankId: [
      {
        required: true,
        message: "请选择借款银行",
        trigger: "change",
      },
    ],
    storeId: [
      {
        required: true,
        message: "请选择仓库",
        trigger: "change",
      },
    ],
    carId: [
      {
        required: true,
        message: "请选择车辆信息",
        trigger: "change",
      },
    ],
    price: [
      {
        required: true,
        message: "请输入车辆单价",
        trigger: "blur",
      },
    ],
    vin: [
      {
        required: true,
        message: "请输入车架号",
        trigger: "blur",
      },
    ],
  });
  const k = ["brand", "series", "model"];
  return {
    bankList,
    storeList,
    carList,
    handleVinDel,
    handleVinAdd,
    handleCarDel,
    handleCarAdd,
    getCarItemsSum,
    getCarListSum,
    rules,
    k
  };
};

export const getCarModels = (list) => {
  const newArray = [];
  const newArray2 = [];
  list.forEach((item) => {
    const repeat = newArray.find((res) => res.label === item.brand);
    if (!repeat) {
      newArray.push({
        value: item.brand,
        label: item.brand,
        children: [item],
      });
    } else {
      repeat.children.push(item);
    }
  });

  newArray.forEach((item) => {
    item.children.forEach((i) => {
      const repeat = newArray2.find((res) => res.label === i.series);
      if (!repeat) {
        newArray2.push({
          value: i.series,
          label: i.series,
          brand: i.brand,
          children: [
            {
              value: i.id,
              label: i.model,
            },
          ],
        });
      } else {
        repeat.children.push({
          value: i.id,
          label: i.model,
        });
      }
    });
  });
  newArray.forEach((item) => {
    item.children.length = 0;
    newArray2.forEach((i) => {
      if (item.label == i.brand) {
       
        item.children.push({
          value: i.value,
          label: i.label,
          children: i.children,
        });
      }
    });
  });
  return newArray;
};
